#include <stdlib.h>

#include <iostream>

#include "SendManager.hpp"
#include <sys/time.h>

/**
 * constructor 
 **/
SendManager::SendManager(ClientManager* clientManager)
{
	this->clientManager=clientManager;
}

/**
 * TODO : destructor 
 **/
SendManager::~SendManager()
{

}

/**
 * run method
 **/
void SendManager::run()
{
	/* time vars for send rate calc */
	struct timeval* tv_start=(struct timeval*)calloc(1,sizeof(struct timeval));
	struct timeval* tv_end=(struct timeval*)calloc(1,sizeof(struct timeval));
	struct timezone* tz=(struct timezone*)calloc(1,sizeof(struct timezone));
	gettimeofday(tv_start,tz);
	long time;


	while(true)
	{
		// wait LOOP_USEC between each loop (cf SendManager.hpp)
		gettimeofday(tv_end,tz);
		time=((tv_end->tv_usec+(tv_end->tv_sec*1000000))-(tv_start->tv_usec+(tv_start->tv_sec*1000000)));
		time=LOOP_USEC-time;
		if(time>0)
			usleep(time);
		gettimeofday(tv_start,tz);
		std::cout << "SendManager loop ..." << std::endl;

		// TODO : loop all the clients (ClientManager)
		// TODO : lock the client (sfd) ?
		// TODO : lock the sendList
		// TODO : prepare the sendMessage
		// TODO : clear the sendList
		// TODO : unlock the sendList
		// TODO : send the message
		// TODO : unlock the client (sfd) ?
	}
}
